package models

import (
	"exam4_9/conf"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB
var err error

func MysqlLink() {
	Dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", conf.AppConfig.Mysql.User, conf.AppConfig.Mysql.Pass, conf.AppConfig.Mysql.Host, conf.AppConfig.Mysql.Port, conf.AppConfig.Mysql.Database)
	DB, err = gorm.Open(mysql.Open(Dsn))
	if err != nil {
		panic("error form mysql database")
	}
	if err = DB.AutoMigrate(&News{}); err != nil {
		panic("error form mysql table")
	}
	fmt.Println("数据库处理完毕")
}
